package com.scrx.ymg.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.scrx.ymg.entity.AgentDrawings;
import com.scrx.ymg.entity.Distribution;
import com.scrx.ymg.entity.User;
import com.scrx.ymg.service.direct.IDirectService;
import com.scrx.ymg.service.distribution.IDistributionService;
import com.scrx.ymg.service.supermarket.ISuperMarketService;
import com.scrx.ymg.service.user.IUserService;

@Controller
@RequestMapping("/userAction")
public class UserAction {
	
	@Autowired
	private IUserService us;
	@Autowired
	private IDistributionService dbs;
	@Autowired
	private ISuperMarketService sms;
	
	@RequestMapping("/hello")
	public String hello(){
		System.out.println("进入");
		List<User> list=us.hello();
		//List<User> list=us.findAll();
		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i).getUserName());
		}
		return "index.jsp";
	}
	public String supertest(){
		return "";
	}
	
	/**
	 * 后台登陆
	 * @param username
	 * @param password
	 * @return
	 */
	@RequestMapping("/login")
	public String login(String username,String password){
		           
		List<User> list=us.hello();
		//List<User> list=us.findAll();
		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i).getUserName());
		}
		return "index.jsp";
	}
	
	/**
	 * 微信小程序登录
	 */
	public void wxlogin(String add,String lon,String lat){
		User user=new User();
		us.save(user);
		
	}
	
	
	/**
	 * 根据超市id查询用户集合
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value ="/getMemberInfo",produces = "text/json;charset=UTF-8")
	public String getMemberInfo(int superId){
		List<User> user = us.getMemberInfoBySuperID(superId);
		Gson gson = new Gson();
		return gson.toJson(user);
	}
	
	/**
	 * 查询用户信息
	 * @param user
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getUserInfoHead", produces = "text/json;charset=UTF-8")
	public String getUserInfoHead(User user,String agentRegion,String superName, int pageNum, int pageCount) {
		Map<Object, Object> map = us.getUserInfo(user,agentRegion,superName, pageNum, pageCount);
		Gson gson = new Gson();
		return gson.toJson(map);
	}
	
	/**
	 * 查询用户信息
	 * @param user
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getUsers", produces = "text/json;charset=UTF-8")
	public String getUsers(User user,int pageNum, int pageCount) {
		List<User> map = us.getUsers(user, pageNum, pageCount);
		Gson gson = new Gson();
		return gson.toJson(map);
	} 
	
	/**
	 * 查询用户信息
	 * @param user
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getUserXiaXian", produces = "text/json;charset=UTF-8")
	public String getUserXiaXian(int userId) {
		Map<Object, Object> map =us.getUserXiaXian(userId);
		Gson gson = new Gson();
		return gson.toJson(map);
	}
	
	
	
	/**
	 * 更新用户信息
	 * @param user
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/updateUserPhone", produces = "text/json;charset=UTF-8")
	public String updateUserPhone(String id,String phone){
		User olduser = us.findById(Integer.parseInt(id));
		olduser.setPhone(phone); 
		return us.updateUser(olduser);
	}
	
	/**
	 * 得到用户信息
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getUserInfoById", produces = "text/json;charset=UTF-8")
	public String getUserInfoById(String UUID){
		User user = us.findById(Integer.parseInt(UUID));
		Gson gson = new Gson();
		return gson.toJson(user);
	}
	
	
	/**
	 * 得到用户分销信息
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getUserDistribution", produces = "text/json;charset=UTF-8")
	public String getUserDistribution(int userID){
		Distribution distribution =dbs.getDistribution(userID);
		Gson gson = new Gson();
		return gson.toJson(distribution);
	}
	
	/**
	 * 用户分销提现申请
	 * 
	 * @param superId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/userDrawings", produces = "text/json;charset=UTF-8")
	public String userDrawings(AgentDrawings drawing) {
		String msg = us.userDrawings(drawing);
		Gson gson = new Gson();
		return gson.toJson(msg);
	}

	/**
	 * 查询提现记录
	 * 
	 * @param drawing
	 * @param pageNum
	 * @param pageCount
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getAgentDrawings", produces = "text/json;charset=UTF-8")
	public String getAgentDrawings(AgentDrawings drawing, int pageNum, int pageCount) {
		Map<Object, Object> map = sms.getAgentDrawings(drawing, pageNum, pageCount);
		Gson gson = new Gson();
		return gson.toJson(map);
	}

	/**
	 * 后台确认提现修改用户佣金
	 * 
	 * @param drawing
	 * @param pageNum
	 * @param pageCount
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/updateDrawings", produces = "text/json;charset=UTF-8")
	public String updateDrawings(int drawingId, int status) {
		List<Object> objects = new ArrayList<Object>();
		AgentDrawings agentDrawings = sms.getAgentDrawings(drawingId);
		agentDrawings.setStatus(status);
		Distribution distribution = dbs.getDistribution(agentDrawings.getMerchantId());
		if (status == 1) {
			distribution.setTxBrokerage(distribution.getTxBrokerage() + agentDrawings.getWithdrawMoney());
		} else {
			distribution.setKtxBrokerage(agentDrawings.getWithdrawMoney());
		}
		objects.add(agentDrawings);
		objects.add(distribution);
		return us.updateObjects(objects);
	}
	

}
